package com.yanqu.road.server.manager.unioncampwar;

import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionCampWarActivityWeek {
    private int weekId;
    /**
     * key:centerGroupId
     */
    private Map<Integer, CrossUnionCampWarActivityGroup> centerGroupMap = new ConcurrentHashMap<>();

    public CrossUnionCampWarActivityWeek(int weekId) {
        this.weekId = weekId;
    }

    public int getWeekId() {
        return weekId;
    }

    public Map<Integer, CrossUnionCampWarActivityGroup> getCenterGroupMap() {
        return centerGroupMap;
    }

    public void setCenterGroupMap(Map<Integer, CrossUnionCampWarActivityGroup> centerGroupMap) {
        this.centerGroupMap = centerGroupMap;
    }

    public UnionCampWarActivity getActivity(int centerGroupId, int groupId) {
        CrossUnionCampWarActivityGroup activityGroup = centerGroupMap.get(centerGroupId);
        if (null == activityGroup) {
            return null;
        }
        return activityGroup.getActivity(groupId);
    }

    public void addActivity(UnionCampWarActivity activity) {
        CrossUnionCampWarActivityGroup activityGroup = centerGroupMap.get(activity.getCenterGroupId());
        if (null == activityGroup) {
            synchronized (centerGroupMap) {
                activityGroup = centerGroupMap.get(activity.getCenterGroupId());
                if (null == activityGroup) {
                    activityGroup = new CrossUnionCampWarActivityGroup(activity.getCenterGroupId(), activity.getWeekId());
                    centerGroupMap.put(activity.getCenterGroupId(), activityGroup);
                }
            }
        }
        activityGroup.addActivity(activity);
    }

    public List<UnionCampWarActivity> getActivityList() {
        List<UnionCampWarActivity> list = new ArrayList<>();
        for (CrossUnionCampWarActivityGroup activityGroup : centerGroupMap.values()) {
            list.addAll(activityGroup.getActivityList());
        }

        return list;
    }

    public CrossUnionCampWarActivityGroup getActivityGroup(int centerGroupId, boolean newIfnull) {
        CrossUnionCampWarActivityGroup activityGroup = centerGroupMap.get(centerGroupId);
        if (null == activityGroup && newIfnull) {
            synchronized (centerGroupMap) {
                activityGroup = centerGroupMap.get(centerGroupId);
                if (null == activityGroup) {
                    activityGroup = new CrossUnionCampWarActivityGroup(centerGroupId, GameConfig.getSystemThisWeekId());
                    centerGroupMap.put(centerGroupId, activityGroup);
                }
            }
        }
        return activityGroup;
    }

    public void save() {
        List<CrossUnionCampWarActivityGroup> groupList = new ArrayList<>(centerGroupMap.values());
        for (CrossUnionCampWarActivityGroup activityGroup : groupList) {
            activityGroup.save();
        }
    }
}
